package com.smec.remes.module.search.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.hwangjr.rxbus.Bus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewSearchBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.ImageDialog;
import com.smec.remes.util.RollerDialog;

import java.util.ArrayList;
import java.util.HashMap;

import static com.smec.remes.module.search.SmecSearchActivity.BUY_NOT_NULL;

/**
 * Created by Administrator on 2017/11/28.
 */

public class SmecSearchAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private ArrayList<AbleMatnr> buycarList=new ArrayList<>();
    private ImageDialog imageDialog;
    private RollerDialog rollerDialog;
    private Bus mRxbus;

    public ArrayList<AbleMatnr> getBuycarList(){
        return buycarList;
    }

    public void setBuycarList(ArrayList<AbleMatnr> buycarList) {
        if(CommonUtils.notEmpty(buycarList)){
            this.buycarList = buycarList;
        }else {
            this.buycarList=new ArrayList<>();
        }
    }

    private Context mcontext;

    public SmecSearchAdapter(Context context,Bus smecBus) {
        this.mcontext=context;
        this.mRxbus=smecBus;
    }

    public void setArrayList(ArrayList<AbleMatnr> list,String status){
        if(status.equals("onRefresh")){
            arrayList=list;
        }else {
            arrayList.addAll(list);
        }

        notifyDataSetChanged();
    }

    public void _clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public void _setBuyList(ArrayList<AbleMatnr> list){
        if(CommonUtils.notEmpty(list)){
            buycarList.addAll(list);
        }
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewSearchBinding layoutRecyclerviewSearchBinding= DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_search,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewSearchBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewSearchBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        final LayoutRecyclerviewSearchBinding layoutRecyclerviewSearchBinding= (LayoutRecyclerviewSearchBinding) holder.getBinding();
        layoutRecyclerviewSearchBinding.setVariable(BR.search,arrayList.get(position));


        layoutRecyclerviewSearchBinding.tvPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layoutRecyclerviewSearchBinding.tvPrice.setText(arrayList.get(position).getMatnrPrice());
                layoutRecyclerviewSearchBinding.tvPrice.setTextColor(mcontext.getResources().getColor(R.color.materiel_text));
            }
        });

        layoutRecyclerviewSearchBinding.llAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rollerDialog=new RollerDialog(mcontext,R.style.project_back);
                rollerDialog.show();
                rollerDialog.reduction();
                TextView textView= (TextView) rollerDialog.findViewById(R.id.tv_roll_mantr);
                textView.setText(arrayList.get(position).getMatnr());
                Button button= (Button) rollerDialog.findViewById(R.id.btn_sure);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rollerDialog.Num().equals("0")){
                            Toast.makeText(mcontext,"选取数量请大于零",Toast.LENGTH_SHORT).show();
                        }else {
                            try {
                                arrayList.get(position).setNum(rollerDialog.Num());
                                String a[] = arrayList.get(position).getMatnrPrice()==null ? new String[]{}: arrayList.get(position).getMatnrPrice().split(" ");
                                if(a[0]!=null){
                                    String price=a[0].trim();
                                    Double i=Double.valueOf(price)*Integer.valueOf(rollerDialog.Num());
                                    if(a[2]!=null){
                                        i=i/Double.valueOf(a[3]);
                                    }
                                    if(!i.isNaN()){
                                        arrayList.get(position).setPriceALL(String.format("%.2f",i));
                                    }else {
                                        arrayList.get(position).setPriceALL("0");
                                    }
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            int[] location = new int[2];
                            layoutRecyclerviewSearchBinding.ivItemadd.getLocationOnScreen(location);
                            _checkCar(arrayList.get(position),location);
                        }
                        rollerDialog.dismiss();
                    }
                });
            }
        });

        layoutRecyclerviewSearchBinding.ivItemadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rollerDialog=new RollerDialog(mcontext,R.style.project_back);
                rollerDialog.show();
                rollerDialog.reduction();
                TextView textView= (TextView) rollerDialog.findViewById(R.id.tv_roll_mantr);
                textView.setText(arrayList.get(position).getMatnr());
                Button button= (Button) rollerDialog.findViewById(R.id.btn_sure);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rollerDialog.Num().equals("0")){
                            Toast.makeText(mcontext,"选取数量请大于零",Toast.LENGTH_SHORT).show();
                        }else {
                            arrayList.get(position).setNum(rollerDialog.Num());
                            String a[] = arrayList.get(position).getMatnrPrice()==null ?
                                    new String[]{} : arrayList.get(position).getMatnrPrice().split(" ");
                            try {
                                if(a[0]!=null){
                                    String price=a[0].trim();
                                    Double i=Double.valueOf(price)*Integer.valueOf(rollerDialog.Num());
                                    if(a[2]!=null){
                                        i=i/Double.valueOf(a[3]);
                                    }
                                    if(!i.isNaN()){
                                        arrayList.get(position).setPriceALL(String.format("%.2f",i));
                                    }else {
                                        arrayList.get(position).setPriceALL("0");
                                    }

                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            int[] location = new int[2];
                            layoutRecyclerviewSearchBinding.ivItemadd.getLocationOnScreen(location);
                            _checkCar(arrayList.get(position),location);
                        }
                        rollerDialog.dismiss();
                    }
                });
            }
        });

        if(TextUtils.isEmpty(arrayList.get(position).getChangeMessage())){
            layoutRecyclerviewSearchBinding.llChange.setVisibility(View.GONE);
        }else {
            layoutRecyclerviewSearchBinding.llChange.setVisibility(View.VISIBLE);
        }

        if(TextUtils.isEmpty(arrayList.get(position).getImageLink())){
            layoutRecyclerviewSearchBinding.llBitmap.setVisibility(View.GONE);
        }else {
            layoutRecyclerviewSearchBinding.llBitmapurl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!TextUtils.isEmpty(arrayList.get(position).getImageLink())){
                        imageDialog=new ImageDialog(mcontext,R.style.project_back);
                        ImageView imageView= (ImageView) imageDialog.getCustomView().findViewById(R.id.iv_image);
                        Glide.with(mcontext)
                                .load(arrayList.get(position).getImageLink())
                                .error(R.drawable.smec_glide_error)
                                .into(imageView);
                        imageDialog.show();
                    }else {
                        Toast.makeText(mcontext,"没有图片",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    private void _checkCar(AbleMatnr ableMatnr,int[] location){
        boolean isHave=false;
        if(CommonUtils.notEmpty(buycarList)){
            for(int i=buycarList.size()-1;i>=0;i--){
                AbleMatnr matnr=buycarList.get(i);
                if(matnr.getMatnr()!=null && ableMatnr.getMatnr()!=null && matnr.getMatnr().equals(ableMatnr.getMatnr())){
                    isHave=true;
                    buycarList.remove(i);
                    buycarList.add(ableMatnr);
                    break;
                }
            }
        }
        if(!isHave){
            buycarList.add(ableMatnr);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("size",new String(buycarList.size()+""));
        hashMap.put("x",location[0]);
        hashMap.put("y",location[1]);
        mRxbus.post(BUY_NOT_NULL, hashMap);
    }

}
